<template>
	<view class="rule-intro page">
		<!-- <to-pre-page :url='url' class="toPrePaage"></to-pre-page> -->
		<view class="head-img">
			<image src="../../static/exam/examIntroHead.jpg"></image>
		</view>
		<view class="content">
			<view class="box">
				<view class="title"></view>
				<view class="cont">
					<text>
						大学生安全知识竞赛是针对全国在校大学生开展的线上考试项目，学生先点击“我的-注册-登录”。
					</text>
					<text v-if="examInfoCenter"> 
						进入答题区后，先支付，后答题，费用{{examInfoData.examCost}}元，限时{{examInfoData.examTimeLimit}}分钟，限制考试次数{{examInfoData.examNumLimit}}次，总分{{examInfoData.totalScore}}分。
					</text>
					<text>
						学生可在“我的-我的考试记录”中查看答题记录，在“我的-我的成绩”中查看最高分。
					</text>
				</view>
			</view>
			<button :loading="loading" class="btn" @click="answer">开始答题</button>
		</view>
	</view>
</template>

<script>
	import toPrePage from "../../components/to-index-page/to-index-page.vue"
	import moment from "moment"
	import Home from "../../api/home/home.js"
	export default {
		data() {
			return {
				url: '/pages/exam/exam',
				examInfoData: {
					examCost: 0,
					examTimeLimit: 0,
					examNumLimit: 0,
					totalScore: 0
				},
				loading: false,
				examInfoCenter: true,
				// 接口还没请求完，不能进行二次请求
				hasStartExam: false
			}
		},
		components:{
			toPrePage
		},
		onLoad() {
			this.examInfo()
			
		},
		methods: {
			answer(){
				if(uni.getStorageSync("token")){
					console.log(this.hasStartExam,'this.hasStartExam')
					if(this.hasStartExam === false){
						this.hasStartExam = true
						this.loading = true
						let params = {
							examType: 0
						}
						Home.getExam(params).then(res=>{
							if(res.code===0){
								this.hasStartExam = false
								console.log(res.data,'考试内容')
								this.loading = false
								if(res.data.userExamStatus===2){
									this.examInfoCenter = true
									uni.showModal({
										title: '考试次数已用完',
										content: '可以在我的-我的考试记录中查看考试详情'
									})
								}else if(res.data.userExamStatus===1){
									this.examInfoCenter = false
									uni.showModal({
										title: '该部门当前没有考试',
										content: '请咨询相关负责人，确认考试的开始时间和结束时间'
									})
								}else{
									this.examInfoCenter = true
									if(res.data.examStatus===1){
										// 如果试卷之前答过，进入试卷，直接显示当前题答案
										let val = {
											examRecordId: res.data.recordId,
											examSort: res.data.smallQuesSort
										}
										Home.statusQues(val).then(val=>{
											if(val.code===0){
												if(val.data!==''){
													uni.navigateTo({
														url:'/pages/exam-question/exam-question?exam='+ encodeURIComponent(JSON.stringify(res.data))+'&hasStart='+true+'&answer='+val.data
													})
												}else{
													uni.navigateTo({
														url:'/pages/exam-question/exam-question?exam='+ encodeURIComponent(JSON.stringify(res.data))+'&hasStart='+false
													})
												}
											}
										})
									}else if(res.data.examStatus===0){
										uni.navigateTo({
											url:'/pages/exam-question/exam-question?exam='+ encodeURIComponent(JSON.stringify(res.data))+'&hasStart='+false
										})
										Home.beginExam(res.data.recordId).then(res=>{
											
										})
									}
								}
							}
						})
					}
				}else{
					this.examInfoCenter = false
					uni.showModal({
						title: '您还未登录',
						content: '您目前为未登录状态，点击确定，进入登录界面',
						success(res) {
							if(res.confirm){
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}
						}
					})
				}
			},
			examInfo(){
				if(uni.getStorageSync("token")){
					this.examInfoCenter = true
					let params = {
						examType: 0
					}
					Home.examInfo(params).then(res=>{
						if(res.code===0){
							if(res.data===null){
								this.examInfoCenter = false
								uni.showModal({
									title: '该部门当前没有考试',
									content: '请咨询相关负责人，确认考试的开始时间和结束时间'
								})
							}else{
								this.examInfoData = res.data
								this.examInfoData.beginTime = moment.unix(this.examInfoData.beginTime/1000).format("YYYY-MM-DD HH:mm:ss")
								this.examInfoData.endTime = moment.unix(this.examInfoData.endTime/1000).format("YYYY-MM-DD HH:mm:ss")
								this.examInfoData.examTimeLimit = this.examInfoData.examTimeLimit/60
							}
						}
					})
				}else{
					this.examInfoCenter = false
					uni.showModal({
						title: '您还未登录',
						content: '您目前为未登录状态，点击确定，进入登录界面',
						success(res) {
							if(res.confirm){
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}
						}
					})
				}
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	.rule-intro{
		.head-img{
			width: 750rpx;
			height: 375rpx;
			image{
				height: 100%;
				width: 100%;
			}
		}
		.content{
			width: 100%;
			.box{
				width: 650rpx;
				height: 550rpx;
				background-color: rgb(255, 255, 255);
				color: #4fb6be;
				margin: 60rpx auto 80rpx;
				box-sizing: border-box;
				padding-bottom: 20rpx;
				overflow: hidden;
				.title{
					   width: 40%;
					   height: 100rpx;
					   margin: auto;
					   background-image: url(../../static/borderImage/borderImage.png);
					   background-repeat: no-repeat;
					   text-align: center;
				}
				.cont{
					height: 378rpx;
					overflow: hidden;
					margin: 0 30rpx 20rpx 30rpx;
					font-size: 30rpx;
					line-height: 42rpx;
					text{
						display: block;
						text-align: left;
						text-indent: 2em;
					}
				}
			}
			.btn{
				width: 650rpx;
				height: 100rpx;
				line-height: 100rpx;
				margin: 0 auto;
				color: #fff;
				background-color: rgb(225, 42, 42);
			}
		}
	}
</style>
